package com.eric.nioclient.service.packet;

import java.nio.charset.Charset;

import io.netty.buffer.ByteBuf;

/**
 * Created by eric on 2017/11/16.
 */
public class UserRegisterPacket extends Packet {
    public String user ; // 用户的名称
    public String room ; // 要进入的房间号

    public UserRegisterPacket(String user,String room){
        this.user = user;
        this.room = room;
    }

    public UserRegisterPacket(){}

    public void writePacketMsg(ByteBuf buf) {
        writeUTF8(buf,user);
        writeUTF8(buf,room);
    }

    public void readFromBuff(ByteBuf buf) {
        user = readUTF8(buf);
        room = readUTF8(buf);
    }

    public PacketType getPacketType() {
        return PacketType.UserRegister;
    }

    public void execPacket() {

    }

    @Override
    protected int getBodyLength() {
        int length = 0 ;
        byte[] bb = user.getBytes(Charset.forName("utf-8"));
        length += bb.length + 4;
        bb = room.getBytes(Charset.forName("utf-8"));
        length += bb.length + 4;

        return length;
    }

    @Override
    public Packet obtain(Object... args) {
        return new UserRegisterPacket(args[0].toString(),args[1].toString());
    }
}
